package com.crux.permission

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.message.BasicNameValuePair

String baseUrl = "https://dfdev.yonyougov.top";
String username = "9993600";
String password = "1";

def client = HttpClientBuilder.newInstance().build();

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES)
        .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
//获取token
String token = getToken(mapper, client, baseUrl, username, password)
//创建用户
JsonNode user = createUser(mapper, client, token, baseUrl)
if (!user) {
    return -1;
}

//启用用户
def enable = enableUser(mapper, client, token, baseUrl, user.get("id").asLong());
if (!enable) {
    return -1;
}






String enableUser(ObjectMapper mapper, CloseableHttpClient httpClient, String token, String baseUrl, long userId) {
    String url = baseUrl + "/crux-uc/api/user/enable?fetchProperties=*,organization[id,lastModifiedVersion,code,name,type],dept[id,lastModifiedVersion,code,name,type],mofdep[id,lastModifiedVersion,code,name,type],credentials[id,lastModifiedVersion,code,name,type],gender[id,lastModifiedVersion,code,name,type]";
    def user = [userId]
    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("Authorization", "Bearer " + token)
    httpPost.addHeader("Context-Type", "application/json")
    StringEntity stringEntity = new StringEntity(mapper.writeValueAsString(user));
    httpPost.setEntity(stringEntity);
    def response = httpClient.execute(httpPost)
    def status = response.getStatusLine().getStatusCode()
    if (status == 200) {
        println "启用用户成功"
        return true
    } else {
        println "启用用户失败"
        return false
    }
}

JsonNode createUser(ObjectMapper mapper, CloseableHttpClient httpClient, String token, String baseUrl) {
    def calendar = Calendar.getInstance();
    String seq = "${calendar.get(Calendar.DAY_OF_MONTH)}-${calendar.get(Calendar.HOUR_OF_DAY)}-${calendar.get(Calendar.MINUTE)}-${calendar.get(Calendar.SECOND)}"
    String url = baseUrl + "/crux-uc/api/user?fetchProperties=*,organization[id,lastModifiedVersion,code,name,type],dept[id,lastModifiedVersion,code,name,type],mofdep[id,lastModifiedVersion,code,name,type],credentials[id,lastModifiedVersion,code,name,type],gender[id,lastModifiedVersion,code,name,type]";
    def user = ["code"        : "user-${seq}",
                "name"        : "user-${seq}",
                "belongType"  : "REGION",
                "organization": "3285292190006616694",
                "office"      : false,
                "enabled"     : false,
                "locked"      : false,
                "photoUrl"    : null];
    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("Authorization", "Bearer " + token)
    httpPost.addHeader("Context-Type", "application/json")
    StringEntity stringEntity = new StringEntity(mapper.writeValueAsString(user));
    httpPost.setEntity(stringEntity);
    def response = httpClient.execute(httpPost)
    def status = response.getStatusLine().getStatusCode()
    if (status == 200) {
        return mapper.readTree(response.getEntity().getContent())
    } else {
        println "创建用户失败"
        return null;
    }

}


String getToken(ObjectMapper mapper, CloseableHttpClient httpClient, String baseUrl, String username, String password) {
    HttpPost post = new HttpPost(baseUrl + "/crux-auth/oauth/token");
    List<BasicNameValuePair> pairs = new ArrayList<>();
    pairs.add(new BasicNameValuePair("username", username));
    pairs.add(new BasicNameValuePair("password", password));
    pairs.add(new BasicNameValuePair("grant_type", "password"));
    pairs.add(new BasicNameValuePair("scope", "read"));
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs);
    post.setEntity(formEntity);
    post.addHeader("Authorization", "Basic bXlpZDpteXNlY3JldA==");
    CloseableHttpResponse response = httpClient.execute(post);
    JsonNode node = mapper.readTree(response.getEntity().getContent());
    return node.get("access_token").asText();
}
